package com.enjoylearning.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.enjoylearning.base.exception.XueChengPlusException;
import com.enjoylearning.content.mapper.CourseBaseMapper;
import com.enjoylearning.content.mapper.CourseTeacherMapper;
import com.enjoylearning.content.model.dto.AddCourseTeacherDto;
import com.enjoylearning.content.model.po.CourseBase;
import com.enjoylearning.content.model.po.CourseTeacher;
import com.enjoylearning.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    @Override
    public List<CourseTeacher> getCourseTeacher(Long id) {
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, id));
        return courseTeachers;
    }

    @Override
    public CourseTeacher addCourseTeacher(AddCourseTeacherDto addCourseTeacherDto) {
        if (Objects.isNull(addCourseTeacherDto)) {
            XueChengPlusException.cast("不能添加空信息");
        }
        // TODO 机构号
        Long companyId = 1L;
        if (!Objects.equals(courseBaseMapper.selectOne(new LambdaQueryWrapper<CourseBase>().eq(CourseBase::getId, addCourseTeacherDto.getCourseId())).getCompanyId(), companyId)) {
            XueChengPlusException.cast("只能修改本机构课程教师！");
        }

        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(addCourseTeacherDto, courseTeacher);
        courseTeacher.setCreateDate(LocalDateTime.now());

        //判断该课程中是否有同名的人  TODO 准确获取添加教师后的主键
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseTeacher.getCourseId()));
        if (courseTeachers.stream().map(CourseTeacher::getTeacherName).collect(Collectors.toList()).contains(courseTeacher.getTeacherName())) {
            XueChengPlusException.cast("不能添加重复教师");
        }

        Integer count = courseTeacherMapper.insert(courseTeacher);
        courseTeacher.setId(courseTeacherMapper.selectOne(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId,courseTeacher.getCourseId())
                .eq(CourseTeacher::getTeacherName,courseTeacher.getTeacherName())).getId());
        return courseTeacher;
    }

    @Override
    public CourseTeacher updateCourseTeacher(CourseTeacher courseTeacher) {
        if (Objects.isNull(courseTeacher)) {
            XueChengPlusException.cast("不能添加空信息");
        }
        // TODO 机构号
        Long companyId = 1L;
        if (!Objects.equals(courseBaseMapper.selectOne(new LambdaQueryWrapper<CourseBase>().eq(CourseBase::getId, courseTeacher.getCourseId())).getCompanyId(), companyId)) {
            XueChengPlusException.cast("只能修改本机构课程教师！");
        }
        courseTeacherMapper.updateById(courseTeacher);
        courseTeacher.setCreateDate(courseTeacherMapper.selectOne(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getId, courseTeacher.getId())).getCreateDate());
        return courseTeacher;
    }

    @Override
    public void deleteCOurseTeacher(Long courseId, Long id) {
        if (courseId == null || id == null) {
            XueChengPlusException.cast("参数错误");
        }
        // TODO 机构号
        Long companyId = 1L;
        if (!Objects.equals(courseBaseMapper.selectOne(new LambdaQueryWrapper<CourseBase>().eq(CourseBase::getId, courseId)).getCompanyId(), companyId)) {
            XueChengPlusException.cast("只能删除本机构课程教师！");
        }
        courseTeacherMapper.deleteById(id);
    }
}
